package cn.jucheng.www.yizhudan.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.jucheng.jclibs.tools.MyLog;
import cn.jucheng.jclibs.tools.MyToast;
import cn.jucheng.jclibs.tools.SubStringUtils;
import cn.jucheng.www.yizhudan.PageActivity;
import cn.jucheng.www.yizhudan.R;
import cn.jucheng.www.yizhudan.base.BaseFragment;
import cn.jucheng.www.yizhudan.base.MyList;
import cn.jucheng.www.yizhudan.callback.MessageEvent;
import cn.jucheng.www.yizhudan.fragment.adapter.MedicalordfragmentAdapter;
import cn.jucheng.www.yizhudan.module.UserMessage;
import cn.jucheng.www.yizhudan.utils.CommUtils;
import cn.jucheng.www.yizhudan.widget.HexadecimalConver;
import cn.jucheng.www.yizhudan.widget.MyGlobal1;
import cn.jucheng.www.yizhudan.widget.MyShareUtils;

/**
 * Created by w on 2017-11-22.
 * 医嘱执行单
 */

public class MedicalordersexecutiveFragment extends BaseFragment implements MedicalordfragmentAdapter.ToastListener, AbsListView.OnScrollListener {


    @BindView(R.id.fragment_medicalordlist)
    MyList fragmentMedicalordlist;

    Unbinder unbinder;
    private View view;

    /**
     * 数据源
     */
    List<String> specailList = new ArrayList<>();
    MedicalordfragmentAdapter adapter;

    public static MyShareUtils datas = null;//缓存数据
    String yiZhuHangHao = "";//医嘱行号
    String yiZhuType = "";//医嘱种类
    int isMessage = 0;//信息类型
    int biaoDanType;//表单类型
    int pages = 1;//页数

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_medicalordersexecutive, null);
        unbinder = ButterKnife.bind(this, view);
//        EventBus.getDefault().register(this);//在当前界面注册一个订阅者
        initView();
        initAdapter();
        return view;
    }

    /**
     * 加载数据
     */
    public void initView() {
        //获取表头信息
        if (datas == null) {
            datas = MyShareUtils.getInstances(getActivity());
            String header_message = datas.getData(MyGlobal1.ALLBIAODANMESSAGE);
            if (!TextUtils.isEmpty(header_message)) {
                setBiaodanHead(header_message);
            }
        }
        //获取表单信息
        if (!TextUtils.isEmpty(UserMessage.biaodan_message)) {
            setBiaodan(UserMessage.biaodan_message);
        }
        getPage();
    }

    /**
     * 初始adapter
     */
    public void initAdapter() {
        adapter = new MedicalordfragmentAdapter(getActivity(), UserMessage.list_Message, pages);
        fragmentMedicalordlist.setAdapter(adapter);
        adapter.setOntoastClickListener(this);
        fragmentMedicalordlist.setOnScrollListener(this);
    }

    /**
     * 处理接受消息的方法  “subscriber methods”
     * 也可以使用注释
     *
     * @param evnt
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(MessageEvent evnt) {
        isMessage = evnt.getIsMessage();
        String message_str = evnt.getMessage();
        setBiaodanMessage(message_str);
        if (biaoDanType == 19) {
            pages = 1;
            getPage();
            adapter.setLists(UserMessage.list_Message, pages);
        }
    }

    /**
     * 计算页数
     */
    public void getPage() {
        int a = UserMessage.medicalord_Message.size() / 10;
        if (UserMessage.medicalord_Message.size() > 10) {
            pages = pages + a;
        }
    }

    /**
     * 解析表头信息
     *
     * @param message
     */
    public void setBiaodanHead(String message) {
        String messaegs = message.substring(0, message.length() - 2);
        UserMessage.list_Message = CommUtils.getJson(HexadecimalConver.toStringHex(messaegs), "baseinfo");
        if (adapter != null) {
            adapter.setLists(UserMessage.list_Message, pages);
        }
    }

    /**
     * 解析表单信息
     *
     * @param message
     */
    public void setBiaodanMessage(String message) {
        switch (isMessage) {
            case 1://1是表单头部信息
                setBiaodanHead(message);
                break;
            case 2://2是清空所有信息
                ClearBiaodanHead();
                break;
            case 3://3是签字信息
                biaoDanType = Integer
                        .parseInt(SubStringUtils.substring(message,
                                52, 54), 16);
                int qianzi_yizhuzhonglei = Integer
                        .parseInt(SubStringUtils.substring(message,
                                54, 56), 16);
                int qianzi_hangHao = Integer
                        .parseInt(SubStringUtils.substring(message,
                                56, 60), 16);
                int qianzi_zhuangTai = Integer
                        .parseInt(SubStringUtils.substring(message,
                                60, 62), 16);

                int hang = 0;
                String nurseName = datas.getData(MyGlobal1.NURSE_NAME);
                if (biaoDanType == 19) {
                    switch (qianzi_zhuangTai) {
                        case 1:
                            if (UserMessage.medicalord_Message.size() > 0) {
                                if (UserMessage.medicalord_MessageMap.containsKey(String.valueOf(qianzi_hangHao))) {//判断是否有key值
                                    MyLog.d("mwh", "medicalord有");
                                    hang = (int) UserMessage.medicalord_MessageMap.get(String.valueOf(qianzi_hangHao));
                                    List<String> list = new ArrayList<>();
                                    list = UserMessage.medicalord_Message.get(hang);
                                    list.set(1, nurseName);
                                    list.set(2, String.valueOf(qianzi_yizhuzhonglei));
                                } else {//数据为0 时
                                    MyLog.d("mwh", "medicalord走了1");
                                    MyToast.showTestToast(getActivity(), getString(R.string.qianzi_faile));
                                }
                            } else {//数据为0 时
                                MyLog.d("mwh", "medicalord走了2");
                                MyToast.showTestToast(getActivity(), getString(R.string.qianzi_faile));
                            }
                            break;
                        case 2:
                            MyLog.d("mwh", "medicalord走了3");
                            MyToast.showTestToast(getActivity(), getString(R.string.qianzi_faile));
                            break;
                        default:
                            break;
                    }
                }
                break;
            case 4://4是表单信息
                setBiaodan(message);
                break;
            default:
                break;
        }
    }

    public void setBiaodan(String message) {
        biaoDanType = Integer
                .parseInt(SubStringUtils.substring(message,
                        52, 54), 16);
        int yiZhuTypes = Integer
                .parseInt(SubStringUtils.substring(message,
                        54, 56), 16);
        int yiZhuHangHaos = Integer
                .parseInt(SubStringUtils.substring(message,
                        56, 60), 16);
        int yiZhuZhuangTai = Integer
                .parseInt(SubStringUtils.substring(message,
                        60, 62), 16);
        int yiZhuchangdus = Integer
                .parseInt(SubStringUtils.substring(message,
                        62, 66), 16);

        int end_lenth = (yiZhuchangdus * 2) + 66;
        yiZhuType = String.valueOf(yiZhuTypes);
        yiZhuHangHao = String.valueOf(yiZhuHangHaos);
        String messaegs = HexadecimalConver.toStringHex(SubStringUtils.substring(message,
                66, end_lenth));
        if (biaoDanType == 19) {
            if (UserMessage.medicalord_MessageMap.size() > 0) {
                if (UserMessage.medicalord_MessageMap.containsKey(yiZhuHangHao)) {//判断是否有key值
                    int hang = (int) UserMessage.medicalord_MessageMap.get(yiZhuHangHao);
                    specailList = new ArrayList<>();
                    specailList = CommUtils.getJson(messaegs, "yizhuzhixingdan");
                    specailList.add(yiZhuType);
                    specailList.add(yiZhuHangHao);
                    UserMessage.medicalord_Message.set(hang, specailList);
                } else {
                    specailList = new ArrayList<>();
                    specailList = CommUtils.getJson(messaegs, "yizhuzhixingdan");
                    specailList.add(yiZhuType);
                    specailList.add(yiZhuHangHao);
                    UserMessage.medicalord_Message.add(specailList);
                    UserMessage.medicalord_MessageMap.put(yiZhuHangHao, (UserMessage.medicalord_Message.size() - 1));//存的行号和list的下标值
                }
            } else {//数据为0 时新添加一条数据
                specailList = new ArrayList<>();
                specailList = CommUtils.getJson(messaegs, "yizhuzhixingdan");
                specailList.add(yiZhuType);
                specailList.add(yiZhuHangHao);
                UserMessage.medicalord_Message.add(specailList);
                UserMessage.medicalord_MessageMap.put(yiZhuHangHao, (UserMessage.medicalord_Message.size() - 1));
            }
        }
    }

    /**
     * 清空本表所有个人信息
     */
    public void ClearBiaodanHead() {
        UserMessage.list_Message.clear();
        UserMessage.medicalord_Message.clear();
        pages = 1;
        adapter.setLists(UserMessage.list_Message, pages);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);//在当前界面注册一个订阅者
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);//取消注册
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
//        EventBus.getDefault().unregister(this);//取消注册
    }

    @Override
    public void onToastClick() {
        MyToast.showTestToast(getActivity(), "请先填写护士姓名.");
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        String number = (firstVisibleItem + 1) + "/" + pages;
        PageActivity.setPageNumber(number);
    }
}
